﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Text;

namespace CowsAndBulls
{
    class Program
    {
        static void Main(string[] args)
        {
            Computer computer = new Computer();
            Player player = new Player();
            ScoreBoard board;
            List<ScoreBoard> list = new List<ScoreBoard>();

            while (!player.Command.Equals("exit"))
            {
                Player.ShowInitMessage();
                computer.GenerateNumber();

                while (true)
                {
                    Console.Write("Enter your guess or command: ");

                    string command = Console.ReadLine();
                    if (!player.GuessNumber(command))
                    {
                        continue;
                    }

                    if (player.EnteredCommand)
                    {
                        if (player.Command.Equals("exit") || player.Command.Equals("restart"))
                        {
                            break;
                        }
                        else
                        {
                            if (player.Command.Equals("help"))
                            {
                                Console.WriteLine(computer.ShowBull());
                                if (computer.Cheats >= 5)
                                {
                                    computer.ResetCheats();
                                    break;
                                }
                            }
                            else
                            {
                                if (player.Command.Equals("top"))
                                {
                                    if (list.Count == 0)
                                    {
                                        Console.WriteLine("Top scoreboard is empty.");
                                        continue;
                                    }
                                    else
                                    {
                                        foreach (ScoreBoard scoreboard in list)
                                        {
                                            Console.WriteLine(scoreboard.ToString());
                                        }
                                    }
                                }
                                else
                                {
                                    Console.WriteLine("You have entered an invalid command!");
                                }
                            }
                        }
                    }
                    else
                    {
                        if (computer.CheckNumber(player.GuessedNumber))
                        {
                            if (computer.Cheats == 0 && (list.Count < 5) ||
                                computer.Cheats == 0 && (list.Last().CountAttempts >= computer.Attempts))
                            {
                                Console.Write("Please enter your name: ");
                                board = new ScoreBoard(computer.Attempts, Console.ReadLine());
                                board.InsertPlayerInScoreBoard(list);
                                computer.ResetAttempts();
                                break;
                            }
                            else
                            {
                                if (computer.Cheats != 0)
                                {
                                    Console.WriteLine("You are not allowed to enter the top scoreboard.");
                                    break;
                                }
                                else
                                {
                                    Console.WriteLine("Your result does not belong to the top scoreboard.");
                                    break;
                                }
                            }
                        }
                        //unvalid number entered - go back to loop beginning
                    }
                }
                computer.ResetAttempts();
                computer.ResetCheats();
            }
            Console.WriteLine("Good bye");
            Console.Write("Press any key to exit...");
            Console.ReadKey(true);
        }
    }
}
